﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using System.IO;
using System.Xml.Serialization;

namespace ZhangZhao.WebAPI
{
    public partial class UserGatherThirdPhoto : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = string.Empty;

            try
            {
                string UserName = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.UserName]);
                string RelationShip = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.RelationShip]);
                int Number = int.MaxValue;
                int.TryParse(HttpContext.Current.Request[Parameter.Number], out Number);
                int StartIndex = 0;
                int.TryParse(HttpContext.Current.Request[Parameter.StartIndex], out StartIndex);

                if (StartIndex == 0)
                    StartIndex = 1;

                string Output = HttpUtility.HtmlEncode(HttpContext.Current.Request[Parameter.Output]);

                if (!string.IsNullOrWhiteSpace(UserName))
                {
                    ZhangZhao.BLL.H_UserGatherThirdPhoto bll = new ZhangZhao.BLL.H_UserGatherThirdPhoto();

                    //用户自己发布的照片
                    string where = string.Format(" UserName = '{0}' and ThirdUserName = '{0}' ", UserName);

                    //关注的用户发布的照片
                    if (RelationShip.Equals("1"))
                        where = string.Format(" UserName = ThirdUserName and UserName in (select OtherUserName from dbo.H_UserRelationship with (nolock) where UserName = '{0}' and RelationShip = '1' ) ", UserName);

                    //用户收藏的照片
                    if (RelationShip.Equals("2"))
                        //where = string.Format(" UserName = '{0}' and UserName <> ThirdUserName ", UserName);
                        where = string.Format(" UserName = '{0}' ", UserName);

                    List<ZhangZhao.Model.H_UserGatherThirdPhoto> list = bll.GetModelListByPage(where, "Id", (StartIndex - 1) * Number, StartIndex * Number);

                    if (!string.IsNullOrWhiteSpace(Output) && Output.Equals("json", StringComparison.CurrentCultureIgnoreCase))
                    {
                        s = JsonConvert.SerializeObject(list);
                    }
                    else
                    {
                        using (MemoryStream Stream = new MemoryStream())
                        {
                            XmlSerializer xml = new XmlSerializer(typeof(List<ZhangZhao.Model.H_UserGatherThirdPhoto>));

                            try
                            {
                                xml.Serialize(Stream, list);
                            }
                            catch { }

                            Stream.Position = 0;
                            using (StreamReader sr = new StreamReader(Stream))
                            {
                                s = sr.ReadToEnd();
                            }

                            xml = null;
                        }
                    }
                }
            }
            catch { }

            if (s.Equals("null", StringComparison.CurrentCultureIgnoreCase))
                s = string.Empty;

            Response.Clear();
            Response.Write(s);
            Response.End();
        }
    }
}